package me.jiatao.paging3.paging3

import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import me.jiatao.paging3.R
import me.jiatao.paging3.network.pojo.ArticleItem
import me.jiatao.paging3.room.ArticleEntity

class ProjectAdapter : PagingDataAdapter<ArticleEntity, ProjectAdapter.ViewHolder>(diffCallback) {
    companion object {
        val diffCallback: DiffUtil.ItemCallback<ArticleEntity> = object : DiffUtil.ItemCallback<ArticleEntity>() {
            override fun areItemsTheSame(oldItem: ArticleEntity, newItem: ArticleEntity): Boolean {
                return false
            }

            override fun areContentsTheSame(oldItem: ArticleEntity, newItem: ArticleEntity): Boolean {
                return false
            }
        }
    }
    
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(parent)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bindTo(getItem(position))
    }


    class ViewHolder(parent: ViewGroup) :
        RecyclerView.ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.view_article, parent, false)) {
        private val titleView = itemView.findViewById<TextView>(R.id.article_title)

        fun bindTo(item: ArticleEntity?) {
            titleView.text = item?.title
        }
    }
}